//文件名：APP_MQTT
//功能：mqtt相关的函数实现
//创建时间：2023年02月06日
//作者：dowson
//email：dowson.tseng@gmail.com


#include "system_init.h"
#include "VehicleOta.h"
#include "JuzhenJson.h"
#include "encryHmac.h"
#include "Base64.h"
#include "APP_MQTT.h"


#define MQTT_INDEX_1_FOR_OTA 1

extern AT_RESTATE AT_state;

static uint8_t mqttState=0;

static tAliMqtt AliMqtt;
// static tMqttSubStruct MqttSubStruct1;  //通道1的缓冲区

extern tAlarmAndErr AlarmAndErr;
extern tSystemState SystemState;
extern tSystemData SystemData;

extern tTaskItem TASK_ITEM_BUFF[MAXCOUNT_TASK_ITEM];

tMqttSubMsgBuffer MqttSubMsgBuffer[MQTT_SUB_BUFFER_SIZE+1]; 
tArrayQueue tMqttRecvQueue;

int8_t GetAliUsername(uint8_t *clientIdBuff,uint16_t buffLen,uint8_t *accessKey);

//mqtt 模块初始化
void MqttPartInit(void){
//创建一个队列用于保存本地实时数据
	ArrayQueue_Inint(&tMqttRecvQueue,MqttSubMsgBuffer,0x01,MQTT_SUB_BUFFER_SIZE,sizeof(tMqttSubMsgBuffer)); 

    VechicleOtaInit();
}


//mqtt数据分析处理函数
//注意：该函数只能进行字符串内容的分割，数据内容不能是源数据，否则将会产生错误
//返回：0 正常，其它 异常
// int8_t MqttDataAnalyse(uint8_t *dataBuf,uint16_t dataLen,tMqttSubMsgBuffer *MqttSubMsgBuffer){
//     // uint8_t mqttMsgHead[] = "+ZMQRCV: ";
//     uint8_t mqttMsgHead[] = "+QMTRECV: ";
//     uint8_t *targetHead = NULL;
//     uint8_t *token = NULL;
//     uint16_t i = 0;
//     targetHead = strstr(dataBuf,mqttMsgHead);
//     targetHead += strlen(mqttMsgHead);
//     if(targetHead){
//         token = strtok(targetHead,",");
//         while (token != NULL)
//         { 
//             switch (i)
//             {
//             case 0:
//                 MqttSubMsgBuffer->mqIndex = atoi(token);
//             break;
//             case 1:
//                 strcpy(MqttSubMsgBuffer->topic,token);
//             break;
//             case 2:
//                 MqttSubMsgBuffer->Qos = atoi(token);
//             break;
//             case 3:
//                 MqttSubMsgBuffer->retained = atoi(token);
//             break;
//             case 4:
//                 MqttSubMsgBuffer->dup = atoi(token);
//             break;    
//             case 5:
//                 MqttSubMsgBuffer->dataLen = atoi(token);
// 			    if(MqttSubMsgBuffer->dataLen < MQTT_SUB_BUFFER_NODE_SIZE){
//                     //memcpy(MqttSubMsgBuffer -> data,token+2,MqttSubMsgBuffer->dataLen);
//                     token += strlen(token)+1;
// 					strcpy(MqttSubMsgBuffer -> data,token);
//                 }
// 				return 0;
//             break;         
//             default:
//                 //无此序号
//             break;
//             }
//             i++;

//             token = strtok(NULL,",");
//         }

//     } 
// 	return -1;
// }
int8_t MqttDataAnalyse(uint8_t *dataBuf,uint16_t dataLen,tMqttSubMsgBuffer *MqttSubMsgBuffer){
    // uint8_t mqttMsgHead[] = "+ZMQRCV: ";
    uint8_t mqttMsgHead[] = "+QMTRECV: ";
    uint8_t *targetHead = NULL;
    uint8_t *token = NULL;
    uint16_t i = 0;
    targetHead = (uint8_t*)strstr((const char*)dataBuf, (const char*)mqttMsgHead);
    targetHead += strlen((const char*)mqttMsgHead);
    if(targetHead){
        token = (uint8_t*)strtok((char*)targetHead,",");
        MqttSubMsgBuffer->dataLen = 0;
        memset(MqttSubMsgBuffer->data, 0x00, sizeof(MqttSubMsgBuffer->data));
        while (token != NULL)
        { 
            switch (i)
            {
            case 0:
                MqttSubMsgBuffer->mqIndex = atoi((const char*)token);
                i++;
            break;
            case 1:
                // strcpy(MqttSubMsgBuffer->topic,token);
                i++;
            break;
            case 2:
                // MqttSubMsgBuffer->Qos = atoi(token);
                strcpy((char*)MqttSubMsgBuffer->topic,(const char*)token);
                i++;
            break;
            case 3:
                // MqttSubMsgBuffer->retained = atoi(token);
                MqttSubMsgBuffer->dataLen += (strlen((const char*)token) + 1);
			    if(MqttSubMsgBuffer->dataLen < MQTT_SUB_BUFFER_NODE_SIZE){
                    // token += strlen(token)+1;
					strcat((char*)(MqttSubMsgBuffer -> data),(const char*)token);
                    strcat((char*)(MqttSubMsgBuffer -> data),",");
                }
            break; 
            default:
                //无此序号
            break;
            }
            token = (uint8_t*)strtok(NULL,",");
        }
        MqttSubMsgBuffer->dataLen -= 5;   // 首引号、末引号、最后一个多余的逗号、\r、\n, 共5个多余字符
        // 往前挪一个字节，因为首字节是引号，需要覆盖掉
        int length = strlen((const char*)(MqttSubMsgBuffer->data));
        for(int i=0;i<length-1;++i){
            MqttSubMsgBuffer->data[i] = MqttSubMsgBuffer->data[i+1];
        }
        printf("MqttDataAnalyse-MqttSubMsgBuffer->data:%s, MqttSubMsgBuffer->dataLen:%d\r\n", MqttSubMsgBuffer->data, MqttSubMsgBuffer->dataLen);
        MqttSubMsgBuffer->data[length-1] = '\0'; 
        MqttSubMsgBuffer->data[length-2] = '\0'; 
        MqttSubMsgBuffer->data[length-3] = '\0'; 
        MqttSubMsgBuffer->data[length-4] = '\0'; 
        MqttSubMsgBuffer->data[length-5] = '\0'; 
        return 0;

    } 
	return -1;
}

//中断话题订阅 AT数据返回，回调函数
//输入：
//输出：
void MqttSubDataCallBack(char *dataBuf, uint32_t dataLen){

    tMqttSubMsgBuffer SubMsgBuffer;
    uint8_t *targetStr = NULL;
    // uint8_t mqttMsgHead[] = "+ZMQRCV: ";
    uint8_t mqttMsgHead[] = "+QMTRECV: ";
    memset(SubMsgBuffer.data,0x00,sizeof(SubMsgBuffer.data));
    targetStr = (uint8_t*)strstr((const char*)dataBuf,(const char*)mqttMsgHead);
    fprintf(USART2_STREAM,"get mqtt msg:%s\r\n\r\n",dataBuf);
    if(targetStr){
        MqttDataAnalyse((uint8_t*)dataBuf,dataLen,&SubMsgBuffer);
        if(SubMsgBuffer.dataLen<10){
            fprintf(USART2_STREAM,"--mqtt no content\r\n");
            memset(SubMsgBuffer.data,0x00,sizeof(SubMsgBuffer.data));
            // Wirelesess_ReceiveBuff_Eliminate();
            return;
        }
        fprintf(USART2_STREAM,"--mqtt index:%d,data len:%d,--\r\n--data:[%s]\n",SubMsgBuffer.mqIndex,SubMsgBuffer.dataLen,SubMsgBuffer.data);
        if(NULL == strstr((const char*)SubMsgBuffer.data,"ota_up")){
            ArrayQueue_AddDataToTail(&tMqttRecvQueue,&SubMsgBuffer);
            printf("--AddDataToTail, MQTT QUEUE STATE:%d\r\n", ArrayQueue_IsEmput(&tMqttRecvQueue));
        }
    }
}


//MQTT初始化函数，创建MQTT连接
void MQTTInitTask(void) {
    static uint8_t funcStep = 1;
    printf("MQTTInitTask-funcStep:%d\r\n", funcStep);
    // static uint8_t chackTimes;
    static uint8_t chackTimes_qmtopen;
    // static uint16_t restartTimes=0;
    uint8_t vehicleVin[20];
    uint8_t hmacCode[64];
    // char hexresult[41];


    // int8_t aliCfgState=-1;
    uint8_t thirdState=0;

    uint8_t mqtt_id=1;
    // uint8_t region=0;
    // uint32_t offset=0;
    // uint8_t* productkey = "123456789";
    // uint8_t* productsecret  = "123456789";
    // uint8_t* devicename  = "device1";
    // uint8_t* clientId = "GID_TBOX_DEV@@@test1";
    // uint8_t* username = "Signature|LTAI5tAunqVBoL1WNCuoecxj|post-cn-zvp2gjt400v";
    // uint8_t* password = "M6p25Qf3rZtoq1tDeyQApQCgS6Q=";
    uint8_t *clientIdHead = "GID_TBOX_DEV@@@";
    uint8_t* accessKey = "LTAI5tAunqVBoL1WNCuoecxj";
    uint8_t* secretKey = "3QQ9hXF6D6iov80ZRCaYbewz7CY6Kk";
    
    uint8_t* server = "post-cn-zvp2gjt400v.mqtt.aliyuncs.com";

    uint32_t port = 1883;


    tTaskItem *thisItem;
    thisItem = FindRunnningItem();

    switch (funcStep)
    {
    // case THIRD_STEP0:
    //     //创建阿里云连接配置
    //     Wirelesess_ReceiveBuff_Eliminate(); //清空数据缓冲区值
    //     CleanResourtBuff();
    //     AliMqttGetInformation(mqtt_id,region,productkey,productsecret,devicename);
    //     funcStep = THIRD_STEP1;
    //     GOToSleep(TASK_AT_WAIT);
    //     break;
    // case THIRD_STEP1:
    //     PushResourtBuff(wireless_rxbuff,strlen(wireless_rxbuff));
    //     //检查阿里云连接配置状态 获得设备密钥
    //     aliCfgState = AliMqttGetInformationReq(&AliMqtt);
    //     if(0 == aliCfgState){
    //         fprintf(USART2_STREAM,"get ali msg-AliMqtt.clientid:%s,AliMqtt.username:%s,AliMqtt.password:%s\r\n",AliMqtt.clientid,AliMqtt.username,AliMqtt.password);
    //         funcStep = THIRD_STEP2;
    //     }else{
    //         funcStep = THIRD_STEP0;
    //     }
    //     SysDelay(100);
    //     break;
    case THIRD_STEP1:
        //设置mqtt协议版本为v3.1.1
        MqttVersion(mqtt_id);
        funcStep = THIRD_STEP2;
        GOToSleep(TASK_AT_WAIT);
        break;
    case THIRD_STEP2:
        //新建mqtt连接
        fprintf(USART2_STREAM,"mqtt-- new\n");
        // Wirelesess_ReceiveBuff_Eliminate(); //清空数据缓冲区值
        // NewMqttConnect(mqtt_id,server,port,10000,1024);
        MqttOpen(mqtt_id, server, port);
        funcStep = THIRD_STEP3;
        GOToSleep(TASK_AT_WAIT);
        break;
    case THIRD_STEP3:
        //检查mqtt连接创建情况
        
        // Wirelesess_Common_Receive();
        if(getQMTOPENFLAG()){
            printf("mqtt--new ok\n");
            chackTimes_qmtopen = 0;
            funcStep = THIRD_STEP4;
        }else{
            if (chackTimes_qmtopen > 20){
                chackTimes_qmtopen = 0;
                funcStep = THIRD_STEP1;
                printf("mqtt--new error, goto first step...\n");
                break;
            }
            printf("mqtt--new error, retry...\n");
            chackTimes_qmtopen ++ ;
        }
        break; 
    case THIRD_STEP4:
        fprintf(USART2_STREAM,"mqtt-- connect\n");
        // Wirelesess_ReceiveBuff_Eliminate(); //清空数据缓冲区值
        //生成mqtt登录信息
        memset(vehicleVin,0x00,sizeof(vehicleVin));
        memset(AliMqtt.username,0x00,sizeof(AliMqtt.username));
        memset(AliMqtt.clientid,0x00,sizeof(AliMqtt.clientid));
        memset(hmacCode,0x00,sizeof(hmacCode));

        GetAliUsername(AliMqtt.username,sizeof(AliMqtt.username),accessKey);
        sprintf((char*)(AliMqtt.clientid), (const char*)clientIdHead);
        if(GetVehicleVin(vehicleVin)){
            //TODO:返回失败，VIN读取异常
        }
        strcat((char*)AliMqtt.clientid,(const char*)vehicleVin);
        //获得密码
        hmac_sha1(secretKey,strlen((const char*)secretKey),AliMqtt.clientid,strlen((const char*)AliMqtt.clientid),hmacCode);
        base64_encode(hmacCode,20,AliMqtt.password,sizeof(AliMqtt.password));
        fprintf(USART2_STREAM,"base64_encode:%s\r\n",AliMqtt.password);
        // for( offset = 0; offset < 20; offset++) {
        //     sprintf( ( hexresult + (2*offset)), "%02x", hmac[offset]&0xff);
        // }
        //连接mqtt
        //MqttConnect(mqtt_id,4,clientId,12000,1,0,NULL,username,password);
        // MqttConnect(mqtt_id,4,AliMqtt.clientid,12000,1,0,NULL,AliMqtt.username,AliMqtt.password);
        MqttConnect(mqtt_id, AliMqtt.clientid, AliMqtt.username, AliMqtt.password);
        funcStep = THIRD_STEP5;
        GOToSleep(TASK_AT_WAIT);
        break;  
    case THIRD_STEP5:
        //检查mqtt连接情况
        // Wirelesess_Common_Receive();
        if(getMqttConnectFlag()){
            funcStep = THIRD_STEP6;
            printf("mqtt connect ok\n");
            MqttConnectStateMark(true);
        }else{
            printf("mqtt connect error\n");
            MqttConnectStateMark(false);
            funcStep = THIRD_STEP1;
        }
        break;  
    case THIRD_STEP6:
        //完成mqtt初始化，启动订阅节点任务
        thirdState = ThirdHasBeenCreat(Mqtt1SubTask);
        if (thirdState==0x01){
            printf("mqtt subsqruest topic and pub msg\n");
            CreatThird("mqsub",To_OS_Slice_Time(50,OS_MSEC),10,Mqtt1SubTask); 
            //MqttSub(mqtt_id,"TID-test-dev/testTopicCmd",0); 
            //delay_ms(30);
            //CreatThird("mqPub",To_OS_Slice_Time(10,OS_SEC),10,MqttDataPublish);  
        } 
        // thirdState = ThirdHasBeenCreat(MqttDataPublish);
        // if (thirdState==0x01) {
        //     CreatThird("mqPub",To_OS_Slice_Time(10,OS_SEC),10,MqttDataPublish);  
        // } 
        //结束当前线程
        funcStep =THIRD_STEP1;
        TaskToClosed(thisItem);
        break;  
    case THIRD_STEP7:
        
        break;     
    default:
        break;
    }

}

//中断数据接收识别检测函数
void MqttSbuIqr(uint8_t* data,uint16_t dataLen){
    switch (MqttReciveCkeck(data))
    {
    case MQTT_INDEX1:
        Mqtt1SubCallBack(data);
        memset(data,0x00,dataLen);
        break;
    
    default:
        break;
    }
}

//通道一回调处理函数
void Mqtt1SubCallBack(uint8_t* data){
    
}
//通道1消息订阅和订阅数据处理任务，需要在中断中将接收到的对应数据放到缓冲中
void Mqtt1SubTask(void){
    //测试数据
    // uint8_t mqtt1_state = 0;
    tMqttSubMsgBuffer SubMsgBuffer;
    extern tSystemState SystemState;
    extern tSystemData SystemData;

    static uint8_t funcStep = 0;

    // static uint8_t chackTimes;
    // static uint16_t restartTimes=0;
    static uint8_t chackTimes_qmtsub;
     uint8_t mqtt_id=1;

    char topic[128];
    tTaskItem *thisItem;
    // thisItem = FindRunnningItem();
    if (funcStep != 2){
        printf("Mqtt1SubTask-funcStep:%d\r\n", funcStep);
    }
    
    switch (funcStep)
    {
    case THIRD_STEP0:
        //订阅主题
        OtaMqttGetTopic(topic,sizeof(topic),MQTT_SUBSCRIBE);
        MqttSub(mqtt_id, 1, (uint8_t *)topic, 0);
        funcStep = THIRD_STEP1;
        GOToSleep(TASK_AT_WAIT);
        break;
    case THIRD_STEP1:
        if(getMqttSubFlag()){
            chackTimes_qmtsub = 0;
            printf("mqtt sub ok\n");
            //发布上线消息
            OtaSubOlineMsgUp(mqtt_id,RTE_GEN_OTA_STATE_ID,"ota_state");
            funcStep =THIRD_STEP2;
            printf("mqtt online request publish ok\n");
        }else{
            chackTimes_qmtsub ++;
            if(chackTimes_qmtsub > 20){
                chackTimes_qmtsub = 0;
                printf("mqtt sub error, end task...\n");
                TaskToClosed(thisItem);
            }else{
                printf("mqtt sub error, retry...\n");
            }
            funcStep = THIRD_STEP0;
        }    
        break;  
    case THIRD_STEP2:
        //开始监控队列是否有数据
        if(!ArrayQueue_IsEmput(&tMqttRecvQueue)){
            if(AT_state == AT_IDLE){
                memset(SubMsgBuffer.data,0x00,strlen((const char*)SubMsgBuffer.data));
                SubMsgBuffer.dataLen = 0;
                SubMsgBuffer.mqIndex = 0;
                ArrayQueue_GetDataFromHead(&tMqttRecvQueue,&SubMsgBuffer);
                printf("--sub mqtt index:%d,data len:%d,--\r\n--data:[%s]\n",SubMsgBuffer.mqIndex,SubMsgBuffer.dataLen,SubMsgBuffer.data);
                if(MQTT_INDEX_1_FOR_OTA == SubMsgBuffer.mqIndex){
                    printf("--in CheckVehicleOtaMsgCallBack\r\n");
                    CheckVehicleOtaMsgCallBack(SubMsgBuffer.data,SubMsgBuffer.dataLen);
                }
            }else{
                printf("ArrayQueue has data but AT NOT IDLE...\r\n");
            }
        }
        //TODO:当MQTT断开连接或者异常时结束任务
        break;
    default:
        break;
    }
}

    // uint8_t mqtt1_state = 0;
    // uint8_t asciiBuff[1024];
    // uint8_t* sendDta = "{test:hello}";

    // static uint8_t funcStep = 0;
    // static uint8_t chackTimes;
    // static uint16_t restartTimes=0;

    //  uint8_t mqtt_id=1;
    //  uint8_t* topic = "TID-test-dev/testTopic";

    // tTaskItem *thisItem;
    // thisItem = FindRunnningItem();
    // fprintf(USART2_STREAM,"mqtt-- push data\n");
    // switch (funcStep)
    // {
    // case THIRD_STEP0:
    //     //发布消息主题
    //     memset(asciiBuff,0x00,sizeof(asciiBuff));
    //     String2Ascii(sendDta,strlen(sendDta),asciiBuff,sizeof(asciiBuff));
    //     fprintf(USART2_STREAM,"send data len:%d-%s\n",strlen(sendDta),sendDta);
    //     MqttPub(mqtt_id,topic,0,0,0,strlen(sendDta),asciiBuff);


//mqtt数据发布任务  10s次
void MqttDataPublish(void){
    // uint8_t mqtt1_state = 0;
    // uint8_t asciiBuff[2048];
    //uint8_t* sendDta = "62537465657266e6789012345678901234567890123456789062537465657266e6789012345678901234567890123456789062537465657266e6789012345678901234567890123456789062537465657266e6789012345678901234567890123456789062537465657266e6789012345678901234567890123456789062537465657266e6789012345678901234567890123456789062537465657266e6789012345678901234567890123456789062537465657266e6789012345678901234567890123456789062537465657266e6789012345678901234567890123456789062537465657266e6789012345678901234567890123456789062537465657266e6789012345678901234567890123456789062537465657266e6789012345678901234567890123456789062537465657266e6789012345678901234567890123456789062537465657266e6789012345678901234567890123456789062537465657266e6789012345678901234567890123456789062537465657266e67890123456789012345678901234567890";
    static uint8_t funcStep = 0;
    // static uint8_t chackTimes;
    // static uint16_t restartTimes=0;
    uint8_t autoData[2048];

    //  uint8_t mqtt_id=1;
    //  uint8_t* topic = "vehicle-dev";

    // tTaskItem *thisItem;
    // thisItem = FindRunnningItem();
    fprintf(USART2_STREAM,"mqtt-- push data\n");
    memset(autoData,0x00,sizeof(autoData));
    //JuzhenJson_GenById(RTE_GEN_JSON_OTA_UP_MSEG_ID,autoData,sizeof(autoData));
    
    switch (funcStep)
    {
    case THIRD_STEP0:
        //发布消息主题
        // memset(asciiBuff,0x00,sizeof(asciiBuff));
        // String2Ascii(sendDta,strlen(sendDta),asciiBuff,sizeof(asciiBuff));
        // fprintf(USART2_STREAM,"send data len:%d-%s\n",strlen(sendDta),sendDta);
        // MqttPub(mqtt_id,topic,0,0,0,strlen(sendDta),asciiBuff);

        //fprintf(USART2_STREAM,"send data len:%d-%s\n",strlen(autoData),autoData);
        //JuzhenJson_GenById(RTE_GEN_JSON_OTA_UP_VED_ID,autoData,sizeof(autoData));
        //MqttPubAscii(mqtt_id,topic,0,0,0,strlen(autoData),autoData);
        funcStep = THIRD_STEP1;
        SysDelay(10);
        break;
    case THIRD_STEP1:
        //JuzhenJson_GenById(RTE_GEN_JSON_OTA_UP_STD_ID,autoData,sizeof(autoData));
        //MqttPubAscii(mqtt_id,topic,0,0,0,strlen(autoData),autoData);
        funcStep = THIRD_STEP2;
        SysDelay(10);
    break;
    case THIRD_STEP2:
        //JuzhenJson_GenById(RTE_GEN_JSON_OTA_UP_EHD_ID,autoData,sizeof(autoData));
        //MqttPubAscii(mqtt_id,topic,0,0,0,strlen(autoData),autoData);
        funcStep = THIRD_STEP3;
        SysDelay(10);
    break;
    case THIRD_STEP3:
        //JuzhenJson_GenById(RTE_GEN_JSON_OTA_UP_MCD_ID,autoData,sizeof(autoData));
        //MqttPubAscii(mqtt_id,topic,0,0,0,strlen(autoData),autoData);
        funcStep = THIRD_STEP4;
        SysDelay(10);
    break;
    case THIRD_STEP4:
        //JuzhenJson_GenById(RTE_GEN_JSON_OTA_UP_BMD_ID,autoData,sizeof(autoData));
        //MqttPubAscii(mqtt_id,topic,0,0,0,strlen(autoData),autoData);
        funcStep = THIRD_STEP5;
        SysDelay(10);
    break;
    case THIRD_STEP5:
        //JuzhenJson_GenById(RTE_GEN_JSON_OTA_UP_LTD_ID,autoData,sizeof(autoData));
        //MqttPubAscii(mqtt_id,topic,0,0,0,strlen(autoData),autoData);
        funcStep = THIRD_STEP6;
        SysDelay(10);
    break;
    case THIRD_STEP6:
        //JuzhenJson_GenById(RTE_GEN_JSON_OTA_UP_AUD_ID,autoData,sizeof(autoData));
        //MqttPubAscii(mqtt_id,topic,0,0,0,strlen(autoData),autoData);
        funcStep = THIRD_STEP0;
    break;

    }    
}

//mqtt断开检测处理中断挂载函数
void MqttDisconnectCheck(uint8_t* buffData){
    if(strstr((const char*)buffData,"+ZMQSTAT: 1,0")){
        mqttState = false;
    }
}
//mqtt连接状态标记
void MqttConnectStateMark(uint8_t state){
    mqttState = state;
}
uint8_t MqttConnectStateGet(void){
    return mqttState;
}   


//字符串转ASCII串，buff必须是字符串长度的两倍
//返回：0 成功  其它 失败
int8_t String2Ascii(uint8_t* srcString,uint16_t stringLen,uint8_t* dstBuff,uint16_t buffLen){
    uint16_t i=0;
    uint8_t data[8];
    if((buffLen/2) <= stringLen){
        return -1;
    }
    for(i=0;i<stringLen;i++){
        memset(data,0x00,sizeof(8));
        // itoa(srcString[i],data,16);
        sprintf((char*)data, "%x", srcString[i]);
        strcat((char*)dstBuff,(const char*)data);
    }
    return 0;
}

//获取ali的userName
int8_t GetAliUsername(uint8_t *clientIdBuff,uint16_t buffLen,uint8_t *accessKey){
    uint8_t *instanceId = "post-cn-zvp2gjt400v";
    uint8_t *authentication_mode = "Signature";
    if(strlen((const char*)instanceId)+strlen((const char*)authentication_mode)+strlen((const char*)accessKey)+3 >buffLen) return -1;
    sprintf((char*)clientIdBuff,"%s",authentication_mode);
    strcat((char*)clientIdBuff,"|");
    strcat((char*)clientIdBuff,(const char*)accessKey);
    strcat((char*)clientIdBuff,"|");
    strcat((char*)clientIdBuff,(const char*)instanceId);
    clientIdBuff[strlen((const char*)clientIdBuff)] = 0;
    return 0;
}

//获得ali的password
int8_t GetAliPassword(uint8_t *mqttPasswordBuff,uint16_t buffLen,uint8_t *clientId){
    return 0;
}


